package com.psc.presenter.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.kpjy.db.gen.DaoMaster;
import com.kpjy.db.gen.DaoSession;
import com.psc.view.utils.log.LogUtil;

import org.greenrobot.greendao.database.Database;

/**
 * Creator:Yx
 * Time:2020/7/29 9:21
 * Description:GreenDao
 */
public class DbManager {
    private static DbManager mDbManager;

    public static DbManager getInstance(Context context) {
        if (null == mDbManager) {
            synchronized (DbManager.class) {
                if (null == mDbManager) {
                    mDbManager = new DbManager(context);
                }
            }
        }
        return mDbManager;
    }

    // 是否加密
    public static final boolean ENCRYPTED = true;

    private Context mContext;

    private final String DB_NAME = "kpjy.db";
    private final String DB_PWD = "666666";

    private DaoMaster.DevOpenHelper mDevOpenHelper;//获取Helper对象
    private DaoMaster mDaoMaster;//DaoMaster
    private DaoSession mDaoSession;//DaoSession

    /*
     *普通数据库
     */
    private DbManager(Context context) {
        this.mContext = context;
        // 初始化数据库信息
        mDevOpenHelper = new DaoMaster.DevOpenHelper(mContext, DB_NAME);
        mDaoMaster = new DaoMaster(getWritableDatabase(mContext));
        mDaoSession = mDaoMaster.newSession();
    }

    /**
     * 加密数据库
     */
    private DbManager(Context context, String mPwd) {
        this.mContext = context;
        mDevOpenHelper = new DaoMaster.DevOpenHelper(mContext, DB_NAME);
        mDaoMaster = new DaoMaster(getWritableDatabase(mContext, mPwd));
        mDaoSession = mDaoMaster.newSession();
    }


    /**
     * 获取普通可读数据库
     *
     * @param context
     * @return
     */
    public SQLiteDatabase getReadableDatabase(Context context) {
        if (null == mDevOpenHelper) {
            getInstance(context);
        }
        return mDevOpenHelper.getReadableDatabase();
    }

    /**
     * 获取普通可写数据库
     *
     * @param context
     * @return
     */
    public SQLiteDatabase getWritableDatabase(Context context) {
        if (null == mDevOpenHelper) {
            getInstance(context);
        }

        return mDevOpenHelper.getWritableDatabase();
    }

    /**
     * 获取加密可读数据库
     */
    public Database getReadableDatabase(Context context, String mPwd) {
        if (null == mDevOpenHelper) {
            getInstance(context);
        }
        return mDevOpenHelper.getEncryptedReadableDb(mPwd);
    }

    /**
     * 获取加密可写数据库
     */
    public Database getWritableDatabase(Context context, String mPwd) {
        if (null == mDevOpenHelper) {
            getInstance(context);
        }
        return mDevOpenHelper.getEncryptedWritableDb(mPwd);
    }

    /**
     * 对数据进行增删改查 使用
     *
     * @return
     */
    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    /**
     * 使用完，关闭
     */
    public void closeHelper() {
        if (mDevOpenHelper != null) {
            mDevOpenHelper.close();
            mDevOpenHelper = null;
        }
        if (mDaoSession != null) {
            mDaoSession.clear();
            mDaoSession = null;
        }
    }

}
